#include <func.h>

void printf_wstatus(int status)
{
    if(WIFEXITED(status))
    {
        int exit_code = WEXITSTATUS(status);
        printf("exit_code = %d",exit_code);
    }
    else if(WIFSIGNALED(status))
    {
        int signo = WTERMSIG(status);
        printf("term_sig = %d",signo);
#ifdef WCOREDUMP
        if(WCOREDUMP(status))
            printf("(core dump)");
#endif
    }
    printf("\n");

}

int main(void)
{
    int pid = fork();

    switch(pid)
    {
        case -1:
            error(1,errno,"fork");
        case 0:
            int time;
            printf("请输入需要几秒后的core文件：");
            scanf("%d",&time);
            sleep(time);

            for(;;);

            abort();
            break;
        default:
            int status;
            pid_t childPid = waitpid(-1,&status,WUNTRACED); 

            if(childPid > 0)
            {
                printf("继续执行后续函数!\n");
                printf_wstatus(status);
            }
            else if(childPid == 0)
            {
                printf("No child changed!\n");
            }
            else
            {
                error(1,0,"waitpid");
            }

            exit(0);
    }
    return 0;
}
